package com.wx.elasticsearch.controller;

import com.wx.elasticsearch.pojo.EsProduct;
import com.wx.elasticsearch.service.EsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author wangxun
 */
@RestController
@RequestMapping("/esProduct")
public class EsProductController {

    @Autowired
    private EsProductService esProductService;

    /**
     * 创建索引
     */
    @PostMapping("/createIndex/{indexName}")
    public boolean createIndex(@PathVariable String indexName) {
        return esProductService.createIndex(indexName);
    }

    /**
     * 索引是否存在
     */
    @PostMapping("/indexExists/{indexName}")
    public boolean indexExists(@PathVariable String indexName) {
        return esProductService.indexExists(indexName);
    }

    /**
     * 删除索引
     */
    @PostMapping("/deleteIndex/{indexName}")
    public boolean deleteIndex(@PathVariable String indexName) {
        return esProductService.deleteIndex(indexName);
    }

    /**
     * 创建映射
     */
    @PostMapping("/createMapping")
    public boolean createMapping() {
        return esProductService.createMapping();
    }

    /**
     * 获取映射
     */
    @PostMapping("/getMapping/{indexName}/{type}")
    public Map<String, Object> getMapping(@PathVariable String indexName, @PathVariable String type) {
        return esProductService.getMapping(indexName, type);
    }

    /**
     * 创建文档
     */
    @PostMapping("/createDocument/{id}")
    public String createDocument(@PathVariable Long id) {
        return esProductService.createDocument(id);
    }

    /**
     * 更新文档
     */
    @PostMapping("/updateDocument/{id}")
    public boolean updateDocument(@PathVariable Long id) {
        return esProductService.updateDocument(id);
    }

    /**
     * 删除文档
     */
    @PostMapping("/deleteDocument/{id}")
    public String deleteDocument(@PathVariable Long id) {
        return esProductService.deleteDocument(id);
    }

    /**
     * 批量创建文档
     */
    @PostMapping("/batchCreateDocument")
    public boolean batchCreateDocument() {
        return esProductService.batchCreateDocument();
    }

    /**
     * 根据 ES 中的 ID 查询
     */
    @PostMapping("/getById/{id}")
    public EsProduct getById(@PathVariable Long id) {
        return esProductService.getById(id);
    }

    /**
     * 分页查询
     */
    @PostMapping("/getByPage/{pageNum}/{pageSize}")
    public List<EsProduct> getByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return esProductService.getByPage(pageNum, pageSize);
    }

    /**
     * 根据 ES 中的 ID 查询
     */
    @PostMapping("/getByIds/{ids}")
    public List<EsProduct> getByIds(@PathVariable String ids) {
        return esProductService.getByIds(ids);
    }

    /**
     * bool must term 精确匹配
     * 查询品牌为 brandName，产品类型为 productCategoryName 的产品集合
     */
    @PostMapping("/getByBrandNameAndProductCategoryName/{brandName}/{productCategoryName}")
    public List<EsProduct> getByBrandNameAndProductCategoryName(@PathVariable String brandName, @PathVariable String productCategoryName) {
        return esProductService.getByBrandNameAndProductCategoryName(brandName, productCategoryName);
    }

    /**
     * bool should term 精确匹配
     * 查询品牌为 brandName 或者产品类型为 productCategoryName 的产品集合
     */
    @PostMapping("/getByBrandNameOrProductCategoryName/{brandName}/{productCategoryName}")
    public List<EsProduct> getByBrandNameOrProductCategoryName(@PathVariable String brandName, @PathVariable String productCategoryName) {
        return esProductService.getByBrandNameOrProductCategoryName(brandName, productCategoryName);
    }

    /**
     * bool should term 精确匹配
     * 查询品牌为 brandName 或者产品类型为 productCategoryName 的产品集合
     */
    @PostMapping("/likeKeywordAndName/{keyword}/{name}")
    public List<EsProduct> likeKeywordAndName(@PathVariable String keyword, @PathVariable String name) {
        return esProductService.likeKeywordAndName(keyword, name);
    }
}
